
var ITEM 	   = "item";
var ITEMS      = "items";
var CAPTION    = "caption";
var CONTENT_ID = "#content";

var DISCOUNT   = 0;

//the loaded items for menu
var itemList   = new Array();

var orderItemList   = new Array();

var cart = new Cart();



/**
 * Order Item
 * 
 * @param itemId
 * @returns {OrderItem}
 */
function OrderItem( itemId)
{
	this.itemId = itemId;
	this.extra  = new Array();
	this.count  = 1;
	this.comment = "";
	this.price;
}

OrderItem.prototype.getItem = function()
{
	if(itemList[this.itemId] != "undefined")
	{
		return itemList[this.itemId];
	}
};

OrderItem.prototype.getCount = function()
{
	return this.count;
};

OrderItem.prototype.countPlus = function()
{
	this.count++;
};

OrderItem.prototype.countMinus = function()
{
	if (this.count > 1)
	{
		this.count--;
	}
};

OrderItem.prototype.getPrice = function()
{
	if(itemList[itemId] != "undefined")
	{
		this.price = itemList[itemId].getPrice();
		
		if(this.extra.length > 0)
		{
			var i = 0;
			for (i ; i > this.extra.length -1 ; i ++)
			{
				this.price += this.extra[i].getPrice();
			}
		}
	}
	
	// price with count
	if (this.count > 1)
	{
		return this.price * this.count;
	}
	else
	{
		return this.price;
	}
};


/**
 * class item
 */
function Item(	id, 
				caption, 
				ingredients, 
				price,
				comment,
				description)
{
	this.id = id;
	this.caption = caption;
	this.ingredients = ingredients;
	this.price = price;
	this.comment = comment;
	this.description = description;
}

/**
 * 
 * @returns price
 */
Item.prototype.getPrice = function()
{
	return this.price;
};

Item.prototype.getId = function()
{
	return this.id;
};

Item.prototype.getCaption = function()
{
	return this.caption;
};

/**
 * Cart
 * @returns {Cart}
 */
function Cart()
{
	this.orderItemList = new Array();
}

Cart.prototype.addOrderItem = function(orderItem)
{
	this.orderItemList.push(orderItem);
};

Cart.prototype.removeOrderItem = function(orderItem)
{
	var index = this.orderItemList.indexOf(orderItem);
	this.orderItemList.splice(index, 1);
};

Cart.prototype.getOrderItemList = function()
{
	return this.orderItemList;
};

/**
 * 
 * @param id
 * @param caption
 * @param price
 * @param count
 * @returns
 */
function createItemWidget(	id, 
							caption, 
							price)
{
	var widget;
	widget = "<div class=\"itemWidget\">";
	widget += "<span>("+id+")</span>";
	widget += "<span>"+caption+"</span>";
	widget +="<span>"+price+"</span>";
	widget +="</div>";
	
	//input count
	widget += "<div>";
//	widget += '		Anzahl <input type="text" value="'+count+'" class="itemWidgetInput"'+id+'">';
	widget += '		<span class="itemWidgetCart" onclick="insertToCart(\''+id+'\')">';
	widget += "			Warenkorb";
	widget += "		</span>";
	
	
	widget += "</div>";
	
	
	
	return widget;
}

/**=======================
 * insert item to cart
 *************************/
function insertToCart(itemId)
{
	//create  order item
	var orderItem = new OrderItem(itemId);
	
	//addOrderItem
	cart.addOrderItem(orderItem);
	
	//test
	alert(cart.getOrderItemList().length);
	
	
	
}
	
/**
 * init event 
 */
function initEventHandler()
{
	$("#ente").click(function(){
		
		$.ajax({
	        type: "GET",
	        url: "content/ente/ente.xml",
	        dataType: "xml",
	        success: xmlParser
	    });
	});
}

/**
 * parse xml to html
 */ 
function xmlParser (xml)
{
	// create ul
	$(CONTENT_ID).append("<ul></ul>");
	
	
	
	$xml = $(xml);
	 $items = $xml.find( ITEM ).each(function () 
	{
		 // create item
		 id = $(this).attr('id');
		 caption = $(this).find( CAPTION ).text();
		 ingredients = [];
		 price = $(this).find( 'price' ).text();
		 comment = $(this).find( 'comment' ).text();
		 description = $(this).find( 'description' ).text();

		 var item = new Item(id, caption, ingredients, price, comment, description);
		 
		 // cache item
		 itemList[id] = item;
		 
		 
		 // add content into DOM
		 $(CONTENT_ID + " ul").append("<li>"+createItemWidget(	item.getId(), 
				 												item.getCaption(), 
				 												item.getPrice())
				 					+"</li>");

		 
		 
	 });
}

